<?php

class Admin_UserGroupController extends Zendvn_Controller_Action {
  
  // Mang tham so nhan duoc o moi Action
  protected $_arrParam;
  
  // Duong dan cua Controller
  protected $_currentController;
  
  // Duong dan cua Action chinh
  protected $_actionMain;
  
  protected $_paginator = array('itemCountPerPage' => 5, 'pageRange' => 3);
  
  protected $_namespace;

  public function init() {
    
    // Mang tham so nhan duoc o moi Action
    $this->_arrParam = $this->_request->getParams();
    
    // Duong dan cua Controller
    $this->_currentController = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller'];
    
    // Duong dan cua Action chinh
    $this->_actionMain = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller'] . '/index';
    
    $this->_paginator['currentPage'] = $this->_request->getParam('page', 1);
    $this->_arrParam['paginator'] = $this->_paginator;
    
    $this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
    $ssFilter = new Zend_Session_Namespace($this->_namespace);
    // $ssFilter->unsetAll();
    if (empty($ssFilter->col)) {
      $ssFilter->keywords = '';
      $ssFilter->col = 'g.order';
      $ssFilter->order = 'ASC';
    }
    
    $this->_arrParam['ssFilter']['keywords'] = $ssFilter->keywords;
    $this->_arrParam['ssFilter']['col'] = $ssFilter->col;
    $this->_arrParam['ssFilter']['order'] = $ssFilter->order;
    
    // Truyen ra view
    $this->view->arrParam = $this->_arrParam;
    $this->view->currentController = $this->_currentController;
    $this->view->actionMain = $this->_actionMain;
    
    $template_path = TEMPLATE_PATH . '/admin/system';
    $this->loadTemplate($template_path, 'template.ini', 'template');
  }

  public function indexAction() {
    
    $this->view->Title = 'User Manager :: User Groups :: List';
    $this->view->headTitle($this->view->Title, true);
    
    $tblUserGroup = new Admin_Model_UserGroup();
    
    $this->view->Items = $tblUserGroup->listItem($this->_arrParam, array('task' => 'usergroup-list'));
    
    $totalItem = $tblUserGroup->countItem($this->_arrParam);
    $paginator = new Zendvn_Paginator();
    $this->view->paginator = $paginator->createPaginator($totalItem, $this->_paginator);
  
  }

  public function addAction() {
    $this->view->Title = 'User Manager :: User Groups :: Add';
    $this->view->headTitle($this->view->Title, true);
    
    if ($this->_request->isPost()) {
      
      $tblUserGroup = new Admin_Model_UserGroup();
      $tblUserGroup->saveItem($this->_arrParam, array('task' => 'usergroup-add'));
      
      $this->_redirect($this->_actionMain);
    }
  
  }

  public function infoAction() {
    $this->view->Title = 'User Manager :: User Groups :: Item Detail';
    $this->view->headTitle($this->view->Title, true);
    $tblUserGroup = new Admin_Model_UserGroup();
    $this->view->Item = $tblUserGroup->getItem($this->_arrParam, array('task' => 'usergroup-info'));
  }

  public function editAction() {
    $this->view->Title = 'User Manager :: User Groups :: Edit';
    $this->view->headTitle($this->view->Title, true);
    $tblUserGroup = new Admin_Model_UserGroup();
    $this->view->Item = $tblUserGroup->getItem($this->_arrParam, array('task' => 'usergroup-edit'));
    
    if ($this->_request->isPost()) {
      $tblUserGroup->saveItem($this->_arrParam, array('task' => 'usergroup-edit'));
      $this->_redirect($this->_actionMain);
    }
  }

  public function deleteAction() {
    $this->view->Title = 'User Manager :: User Groups :: Delete';
    $this->view->headTitle($this->view->Title, true);
    if ($this->_request->isPost()) {
      $tblGroup = new Admin_Model_UserGroup();
      $tblGroup->deleteItem($this->_arrParam, array('task' => 'usergroup-delete'));
      $this->_redirect($this->_actionMain);
    }
  }

  public function statusAction() {
    $tblUserGroup = new Admin_Model_UserGroup();
    $tblUserGroup->changeStatus($this->_arrParam, null);
    $this->_redirect($this->_actionMain);
    $this->_helper->viewRenderer->setNoRender();
  }

  public function multiDeleteAction() {
    
    if ($this->_request->isPost()) {
      
      $tblUserGroup = new Admin_Model_UserGroup();
      $tblUserGroup->deleteItem($this->_arrParam, array('task' => 'usergroup-multi-delete'));
      $this->_redirect($this->_actionMain);
    }
    $this->_helper->viewRenderer->setNoRender();
  }

  public function sortAction() {
    
    if ($this->_request->isPost()) {
      
      $tblUserGroup = new Admin_Model_UserGroup();
      $tblUserGroup->sortItem($this->_arrParam);
      $this->_redirect($this->_actionMain);
    }
    
    $this->_helper->viewRenderer->setNoRender();
  }

  public function filterAction() {
    $ssFilter = new Zend_Session_Namespace($this->_namespace);
    
    if ($this->_arrParam['type'] == 'search') {
      if ($this->_arrParam['key'] == 1) {
        $ssFilter->keywords = trim($this->_arrParam['keywords']);
      } else {
        $ssFilter->keywords = '';
      }
    }
    
    if ($this->_arrParam['type'] == 'order') {
      $ssFilter->col = $this->_arrParam['col'];
      $ssFilter->order = $this->_arrParam['by'];
    }
    
    $this->_redirect($this->_actionMain);
    $this->_helper->viewRenderer->setNoRender();
  }

}